/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Business;

import DataAccess.EventDataAccess;
import DataAccess.RegisterDataAccess;
import Entities.CategoryEntities;
import Entities.EventEntities;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

/**
 *
 * @author vjphl
 */
@ManagedBean
@RequestScoped
public class EventBean {

    List<EventEntities> eventList;
    List<EventEntities> eventListDetail;

    List<EventEntities> eventListAll;
    private String CatId;
    private String EventId;
    private String registerEvent;
    private int check;
    EventEntities eventEntities;
    HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
    
    
    /**
     * Creates a new instance of EventBean
     */
    public EventBean() {
        //eventList = new ArrayList<EventEntities>();
        //eventListDetail = new ArrayList<EventEntities>();
        eventListAll = new ArrayList<EventEntities>();
        eventEntities = new EventEntities();
        if (FacesContext.getCurrentInstance().isPostback()) {  
            registerEvent();
            
        }
            AllEventbyId();
            ViewEventDetail();
            AllEventAll();

    }
    
    public void AllEventAll() {
        try {
            EventDataAccess eventDataAccess = new EventDataAccess();
            
            eventListAll = eventDataAccess.getEventAll();
        } catch (Exception ex) {
            Logger.getLogger(EventBean.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public void AllEventbyId() {
        try {
            CatId =FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("CatId");
            if (FacesContext.getCurrentInstance().isPostback()) {
                if (CatId!=null) {
                    FacesContext.getCurrentInstance().getExternalContext().redirect("../faces/ViewEvent.xhtml?CatId="+CatId);
                }
            }
            String EventId =FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("eventId");
            EventDataAccess eventDataAccess = new EventDataAccess();
            eventList = eventDataAccess.getEventById(CatId);

        } catch (Exception ex) {
            Logger.getLogger(EventBean.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public void ViewEventDetail(){
        try {
            if (FacesContext.getCurrentInstance().isPostback()) {
                EventEntities.id = (FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("eventId"));
            }
            EventDataAccess eventDataAccess = new EventDataAccess();
            eventListDetail = eventDataAccess.getEventDetail(EventEntities.id);
            if (EventEntities.id != null) {
                if (FacesContext.getCurrentInstance().isPostback()) {
                    FacesContext.getCurrentInstance().getExternalContext().redirect("../faces/ViewEventDetail.xhtml");
                }
            }
        } catch (Exception ex) {
            Logger.getLogger(EventBean.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public void registerEvent(){
        try {
            String eventid = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("regisEvent");
            String adherence = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("readherence");
            RegisterDataAccess registerDataAccess = new RegisterDataAccess();
            if (eventid!=null) {
                if (adherence== null) {
                adherence = "Presentation";
                FacesContext.getCurrentInstance().getExternalContext().redirect("../faces/UploadSlide.xhtml");
                }
            }
            if (eventid != null && adherence != null) {
                if (registerDataAccess.registerEvent(eventid,"1",adherence)) {
                FacesMessage error = new FacesMessage("Register Event Success");
                FacesContext.getCurrentInstance().addMessage(null, error);
                } 
            }
        } catch (Exception ex) {
            Logger.getLogger(RegisterBean.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    }
    
    public void urlEvent(){
        try {
            FacesContext.getCurrentInstance().getExternalContext().redirect("../Admin/ViewEvent.xhtml");
        } catch (IOException ex) {
            Logger.getLogger(EventBean.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    public void urlUserEvent(){
        try {
            FacesContext.getCurrentInstance().getExternalContext().redirect("../faces/ViewEvent.xhtml");
        } catch (IOException ex) {
            Logger.getLogger(EventBean.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public void urlEventUser(){
        try {
            FacesContext.getCurrentInstance().getExternalContext().redirect("../faces/ViewEvent.xhtml");
        } catch (IOException ex) {
            Logger.getLogger(EventBean.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public void urlEventDetails(){
        try {
            FacesContext.getCurrentInstance().getExternalContext().redirect("../faces/ViewEventDetail.xhtml");
        } catch (IOException ex) {
            Logger.getLogger(EventBean.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public void urlAddEvent(){
        try {
            FacesContext.getCurrentInstance().getExternalContext().redirect("../Admin/AddEvent.xhtml");
        } catch (IOException ex) {
            Logger.getLogger(EventBean.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public void urlAddCategory(){
        try {
            FacesContext.getCurrentInstance().getExternalContext().redirect("../Admin/AddCategory.xhtml");
        } catch (IOException ex) {
            Logger.getLogger(EventBean.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public String InsertEvent(){
        //String CatId = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("CatId");
        try {
            EventDataAccess eventDataAccess = new EventDataAccess();
            if (eventDataAccess.Insertevent(eventEntities)) {
                return "InsertSuccess";
            }
        } catch (Exception ex) {
            Logger.getLogger(EventBean.class.getName()).log(Level.SEVERE, null, ex);
        }
        return "InsertFalse";
    }

    public List<EventEntities> getEventList() {
        return eventList;
    }

    public void setEventList(List<EventEntities> eventList) {
        this.eventList = eventList;
    }
    
    public EventEntities getEventEntities() {
        return eventEntities;
    }

    public void setEventEntities(EventEntities eventEntities) {
        this.eventEntities = eventEntities;
    }
   
    public List<EventEntities> getEventListAll() {
        return eventListAll;
    }

    public void setEventListAll(List<EventEntities> eventListAll) {
        this.eventListAll = eventListAll;
    }
    
    public String getCatId() {
        return CatId;
    }

    public void setCatId(String CatId) {
        this.CatId = CatId;
    }
    
    public List<EventEntities> getEventListDetail() {
        return eventListDetail;
    }

    public void setEventListDetail(List<EventEntities> eventListDetail) {
        this.eventListDetail = eventListDetail;
    }
}
